package com.huifaapp.mall.selectImage;

import android.graphics.Bitmap;
import android.os.Handler;
import android.support.v4.util.LruCache;
import android.widget.ImageView;


import java.util.concurrent.ExecutorService;

public class LocalImageThread extends Thread {
	String path;
	Bitmap bt;
	int width;
	int height;
	ImageView imageView;
	ILocalImageAsyncTask ilocalImageAsyncTask;
	LruCache<String, Bitmap> lruCache;

	public LocalImageThread(String path) {
		this.path = path;
	}

	public LocalImageThread(String path, int width, int height) {
		this.path = path;
		this.width = width;
		this.height = height;
	}

	public LocalImageThread(String path, int width, int height,
                            ImageView imageView) {
		this.path = path;
		this.width = width;
		this.height = height;
		this.imageView = imageView;
	}

	public void setLruCache(LruCache<String, Bitmap> lruCache) {
		this.lruCache = lruCache;
	}

	public void ExecutorStart(ExecutorService executorService) {
		executorService.execute(this);
	}

	@Override
	public void run() {
		bt = BitmapCondense.Condense(width, height, path);
		if (lruCache != null && bt != null) {
			lruCache.put(path, bt);
		}
		handler.sendEmptyMessage(1);
	};

	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
				case 1:
					if (ilocalImageAsyncTask != null && bt != null) {
						ilocalImageAsyncTask.UIUpdate(path, bt, imageView);
					}
					break;
				default:
					System.out.println("加载图片错误");
			}
		}
	};

	public void setIlocalImageAsyncTask(
			ILocalImageAsyncTask ilocalImageAsyncTask) {
		this.ilocalImageAsyncTask = ilocalImageAsyncTask;
	}

	public interface ILocalImageAsyncTask {
		public void UIUpdate(String path, Bitmap bitmap, ImageView imageView);
	}
}
